package com.moon.tools.util;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.moon.tools.entity.GenInfoEntity;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: AutoGeneDatabaseDocument
 * @Description: 自动生成数据库文档
 * @Author: 郭正洋
 * @Date: 2022/10/11 11:08
 * @Vsersion: 1.0.0
 **/
public class AutoGeneDatabaseDocument {

	public static void genMysqlDocument(GenInfoEntity genInfo){
		// 数据源
		HikariConfig hikariConfig = new HikariConfig();
		hikariConfig.setDriverClassName(genInfo.getDriver());
		hikariConfig.setJdbcUrl(genInfo.getUrl());
		hikariConfig.setUsername(genInfo.getUserName());
		hikariConfig.setPassword(genInfo.getPassword());
		// 设置可以获取tables remarks信息
		hikariConfig.addDataSourceProperty("useInformationSchema", "true");
		hikariConfig.setMinimumIdle(2);
		hikariConfig.setMaximumPoolSize(5);
		DataSource dataSource = new HikariDataSource(hikariConfig);

		// 1、生成文件配置
		EngineConfig engineConfig = EngineConfig.builder()
				// 生成文件路径
				.fileOutputDir(genInfo.getOutputPath())
				// 打开目录
				.openOutputDir(false)
				// 文件类型
				.fileType(EngineFileType.WORD)
				// 生成模板实现
				.produceType(EngineTemplateType.freemarker).build();

		// 忽略表名
		List<String> ignoreTableName = Arrays.asList("test");
		// 忽略表前缀
		List<String> ignorePrefix = Arrays.asList("test_", "test");
		// 忽略表后缀
		List<String> ignoreSuffix = Arrays.asList("_test", "test");

		// 2、配置想要忽略的表
		ProcessConfig processConfig = ProcessConfig.builder().ignoreTableName(ignoreTableName)
				.ignoreTablePrefix(ignorePrefix).ignoreTableSuffix(ignoreSuffix).build();

		// 3、生成文档配置（包含以下自定义版本号、描述等配置连接）
		Configuration config = Configuration.builder().version(genInfo.getVersion()).organization(genInfo.getOrganization()).organizationUrl(genInfo.getOrganizationUrl()).description(genInfo.getDescription()).dataSource(dataSource)
				.engineConfig(engineConfig).produceConfig(processConfig).build();

		// 4、执行生成
		new DocumentationExecute(config).execute();
	}
}
